﻿using PC.AzureMultiApp.WebRoleMultiApp.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using PC.AzureMultiApp.Core.Models;
using System.Web.Mvc;
using PC.AzureMultiApp.Storage.Moles;
using System.Collections.Generic;
using PC.AzureMultiApp.Storage;
using PC.AzureMultiApp.Core.Moles;

namespace PC.AzureMultiApp.WebRoleMultiApp.Tests
{
    [TestClass()]
    public class UpdateControllerTest
    {

        [TestMethod]
        [HostType("Moles")]
        public void UpdateTest()
        {
            UpdateController controller = new UpdateController();

            MApplicationDataSource.Constructor = (a) => { };

            MApplicationDataSource.AllInstances.GetAll = (datasource)
             =>
            {
                return new List<AzureApplicationEntity>() { new AzureApplicationEntity() { UniqueId = "uniqueId" } };
            };


            ViewResult result = controller.Update("uniqueId") as ViewResult;
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [HostType("Moles")]
        public void Update_NoExists_Test()
        {
            UpdateController controller = new UpdateController();

            MApplicationDataSource.Constructor = (a) => { };

            MApplicationDataSource.AllInstances.GetAll = (datasource)
             =>
            {
                return new List<AzureApplicationEntity>() { new AzureApplicationEntity() { UniqueId = "uniqueId" } };
            };


            ViewResult result = controller.Update("no_exists") as ViewResult;
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [HostType("Moles")]
        public void UpdateAppTest()
        {
            ApplicationModel newApp = new ApplicationModel();
            newApp.IsWebDeploy = false;
            newApp.PartialUpdate = false;
            newApp.ApplicationUrl = "newUrl";

            MApplicationDataSource.Constructor = (a) => { };

            UpdateController controller = new UpdateController();

            bool notify = false;
            MDeploymentManager.NotifyApplicationModelBoolean = (a, b)
                =>
            {
                notify = true;
            };

            bool called = false;
            MApplicationDataSource.AllInstances.UpdateStringAzureApplicationEntity = (a, b, app)
                =>
            {
                Assert.IsNotNull(app);
                Assert.AreEqual(app.ApplicationUrl, newApp.ApplicationUrl);
                Assert.IsFalse(app.IsWebDeploy);
                Assert.IsTrue(app.Status == GlobalDef.Updating);
                called = true;
            };

            MApplicationDataSource.AllInstances.GetAll = (datasource)
             =>
            {
                return new List<AzureApplicationEntity>() { new AzureApplicationEntity() { UniqueId = "uniqueId", ApplicationUrl = "previousUrl" } };
            };


            controller.Update("uniqueId", newApp);

            Assert.IsTrue(called);
            Assert.IsTrue(notify);
        }

        [TestMethod]
        [HostType("Moles")]
        public void UpdateApp_PartialUpdate_Test()
        {
            ApplicationModel newApp = new ApplicationModel();
            newApp.IsWebDeploy = false;
            newApp.PartialUpdate = true;
            newApp.ApplicationUrl = "newUrl";

            MApplicationDataSource.Constructor = (a) => { };

            UpdateController controller = new UpdateController();

            bool notify = false;
            MDeploymentManager.NotifyApplicationModelBoolean = (a, b)
                =>
            {
                notify = true;
            };

            bool called = false;
            MApplicationDataSource.AllInstances.UpdateStringAzureApplicationEntity = (a, b, app)
                =>
            {
                Assert.IsNotNull(app);
                Assert.AreEqual(app.ApplicationUrl, "previousUrl");
                Assert.IsFalse(app.IsWebDeploy);
                Assert.IsTrue(app.Status == GlobalDef.Updating);
                called = true;
            };

            MApplicationDataSource.AllInstances.GetAll = (datasource)
             =>
            {
                return new List<AzureApplicationEntity>() { new AzureApplicationEntity() { UniqueId = "uniqueId", ApplicationUrl = "previousUrl" } };
            };


            controller.Update("uniqueId", newApp);

            Assert.IsTrue(called);
            Assert.IsTrue(notify);
        }

        [TestMethod]
        [HostType("Moles")]
        public void UpdateAppNoExistsTest()
        {
            MApplicationDataSource.Constructor = (a) => { };

            UpdateController controller = new UpdateController();

            bool notify = false;
            MDeploymentManager.NotifyApplicationModelBoolean = (a, b)
                =>
            {
                notify = true;
            };

            bool called = false;
            MApplicationDataSource.AllInstances.UpdateStringAzureApplicationEntity = (a, b, app)
                =>
            {
                Assert.IsNotNull(app);
                Assert.IsFalse(app.IsWebDeploy);
                Assert.IsFalse(app.Status == GlobalDef.Updating);
                called = true;
            };

            MApplicationDataSource.AllInstances.GetAll = (datasource)
             =>
            {
                return new List<AzureApplicationEntity>() { new AzureApplicationEntity() { UniqueId = "uniqueId" } };
            };

            ApplicationModel newApp = new ApplicationModel();
            newApp.IsWebDeploy = false;

            controller.Update("no_exist", newApp);

            Assert.IsFalse(called);
            Assert.IsFalse(notify);
        }
    }
}
